// 登录成功后个人资料
(function() {
	var a_width = $(".block-search-user").outerWidth();
	var userAgent = navigator.userAgent.toLowerCase();
	// Figure out what browser is being used 
	jQuery.browser = {
		version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
		safari: /webkit/.test(userAgent),
		opera: /opera/.test(userAgent),
		msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
		mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent)
	};
	if($.browser.msie) {
		a_width = a_width - 4;
	}
	var _right = a_width - 23;
	var _width = a_width + 5

	$(".block-search-user").hover(function() {
		$(".search-user").addClass("hover");
		$(".search-user-name i.up").css("display", "inline-block");
		$(".search-user-name i.down").css("display", "none");
	}, function() {
		$(".search-user").removeClass("hover");
		$(".search-user-name i.up").css("display", "none");
		$(".search-user-name i.down").css("display", "inline-block");
	});
	$(".search-user-top").css("right", _right + "px");
	$(".null").css("width", _width + "px");
})();

avalon.ready(function() {
	var search_key = $("input[name='search_key']").val();
	// 页面头部搜索
	var vm_login_tip = avalon.define({
	    $id: "login_tip",
	    search_key: search_key,
	    search: function(){
	        // 头部搜索
	        key = vm_login_tip.search_key;
	        location.href = APP_ROOT+"/index.php?ctl=live&act=search&key="+key;
	    }
	});
	avalon.scan(document.getElementById('login_tip'));
	
	$("input[name='search_key']").bind('keypress',function(event){  
		console.log(111);
	    if(event.keyCode == "13") vm_login_tip.search();
	});
});

// 保存图片
function save_img(w,h,el,ajax_url){
    var scale_w = parseFloat(w/h);
    var el = el;
    scale_w = scale_w.toFixed(1);
    scale = scale_w+'/1';
    bind_CropAvatar(scale, function(){
        var image_url = $("input[name='"+el+"']").val();
		if(!image_url){
			return;
		}
        var data = {};
        data[el] = image_url;
        handleAjax.handle(ajax_url,data).done(function(msg){
            $.showSuccess(msg);
        }).fail(function(err){
            $.showErr(err);
        });
    });
}
$(function(){
	$(".J-anchor").click(function(){
	    $(this).parent().toggleClass('act');
	});
});


var bmCommonFun = {

	/**
	 * 实例时间选择器
	 * @param  el
	 * @return obj
	 */
	initDateTimePicker(el) {
		$.datetimepicker.setLocale('ch');
		$('#' + el).datetimepicker({
		    timepicker:false,
		    format:"Y-m-d"
		});
	},

	/**
	 * 列表搜索
	 * @param  options (url 请求接口, data 请求数据, list_el 列表对象)
	 * @return  result 返回结果
	 */
	searchList(options){
		let op = { url:'', data:{}, list_el:'.ajax-block', callbackFun:null };
		options = $.extend({},op, options);

		let url_arr = options.url.split('/');
		var vm = avalon.define({
		    $id: "vm-search",
		    search_data: options.data,
		    search() {
		        // 搜素
		        layer.load();
		        $handleAjax.handle({
		            url: TMPL_REAL + '/index.php?ctl=' + url_arr[0] + '&act=' + url_arr[1],
		            isTip: false,
		            dataType: 'html',
		            data: this.search_data,
		            type: 'GET'
		        }).done(function(result){
		            layer.closeAll();
		            var tplElement = $('<div id="tmpHTML"></div>').html(result),
		            htmlObject = tplElement.find(options.list_el),
		            html = $(htmlObject).html();
		            $(document).find(options.list_el).html(html);
		            options.callbackFun && (typeof options.callbackFun == 'function') && options.callbackFun.call(this);
		        }).fail(function(err){
		            console.log(err);
		        });
		    }
		});
	}
}